﻿using System;
using System.Data;
using System.Configuration;
using System.Net;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
/// <summary>
/// 网页类
/// </summary>
namespace Wozuji.Common
{
    public class WebPage
    {

        
        private Uri m_uri;   //网址
      
        private string m_title;        //此网页的标题
        private string m_html;         //此网页的HTML代码
        private string m_outstr;       //此网页可输出的纯文本
        private bool m_good;           //此网页是否可用
        private int m_pagesize;       //此网页的大小
        private static Dictionary<string, CookieContainer> webcookies = new Dictionary<string, CookieContainer>();//存放所有网页的Cookie
        private string m_post;  //此网页的登陆页需要的POST数据
        private string m_loginurl;  //此网页的登陆页
       

        /// <summary>
        /// 此私有方法从一段HTML文本中提取出一定字数的纯文本
        /// </summary>
        /// <param name="instr">HTML代码</param>
        /// <param name="firstN">提取从头数多少个字</param>
        /// <param name="withLink">是否要链接里面的字</param>
        /// <returns>纯文本</returns>
        public string getFirstNchar(string instr, int firstN, bool withLink,string nid)
        {
            
            
            
                m_outstr = instr.Clone() as string;
                m_outstr = new Regex(@"(?m)<script[^>]*>(\w|\W)*?</script[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, "");
                m_outstr = new Regex(@"(?m)<style[^>]*>(\w|\W)*?</style[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, "");
                m_outstr = new Regex(@"(?m)<select[^>]*>(\w|\W)*?</select[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, "");
                if (!withLink) m_outstr = new Regex(@"(?m)<a[^>]*>(\w|\W)*?</a[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, "");
                Regex objReg = new System.Text.RegularExpressions.Regex("(<[^>]+?>)|&nbsp;", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                m_outstr = objReg.Replace(m_outstr, "");
                Regex objReg2 = new System.Text.RegularExpressions.Regex("(\\s)+", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                m_outstr = objReg2.Replace(m_outstr, " ");
           
            return m_outstr.Length > firstN ? m_outstr.Substring(0, firstN)+"...<br><a href='shownote.aspx?nid="+nid+"' style='font-size:14px; color:Gray'><更多></a>" : m_outstr;
        }

        

        /// <summary>
        /// 此私有方法返回一个IP地址对应的无符号整数
        /// </summary>
        /// <param name="x">IP地址</param>
        /// <returns></returns>
        private uint getuintFromIP(IPAddress x)
        {
            Byte[] bt = x.GetAddressBytes();
            uint i = (uint)(bt[0] * 256 * 256 * 256);
            i += (uint)(bt[1] * 256 * 256);
            i += (uint)(bt[2] * 256);
            i += (uint)(bt[3]);
            return i;
        }



        
        /// <summary>
        /// 此公有方法提取网页中一定字数的纯文本，包括链接文字
        /// </summary>
        /// <param name="firstN">字数</param>
        /// <returns></returns>
        public string getContext(string html ,int firstN,string nid)
        {
            return getFirstNchar(html, firstN, false, nid);
        }

        /// <summary>
        /// 此公有方法提取网页中一定字数的纯文本，不包括链接文字
        /// </summary>
        /// <param name="firstN"></param>
        /// <returns></returns>
        public string getContextWithOutLink(int firstN,string nid)
        {
            return getFirstNchar(m_html, firstN, false,nid);
        }




       
    }
}
